package com.gc.db;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DBUtil {

	public String url = "jdbc:oracle:thin:@localhost:1521:MYDB";
	public String driver = "oracle.jdbc.driver.OracleDriver";
	public String username = "gc";
	public String password = "gc";

/*    public String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
    public String driver = "com.mysql.jdbc.Driver";
    public String username = "root";
    public String password = "admin";*/

    Connection conn = null;
    ResultSet tableSet = null;
    DatabaseMetaData databaseMetaData = null;

    public DBUtil(String url, String username, String password, String driver) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.driver = driver;
    }


    public void getConnection() throws SQLException, ClassNotFoundException {
        System.out.println(username + "--" + password);
        Class.forName(driver);
        conn = DriverManager.getConnection(url, username, password);
    }


    public static boolean isConSpeCharacters(String string) {
        if (string.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
            return false;
        }
        return true;
    }
    List<String> tables = new ArrayList<>();
    public List<String> getTableInfo() throws Exception {
        getConnection();
        List<String> list = new ArrayList<>();
        databaseMetaData = conn.getMetaData();
        // 获取所有表
        tableSet = databaseMetaData.getTables(conn.getCatalog(), username.toUpperCase(), null,
                new String[]{"TABLE"});
        // 获取tableName表列信息
        // 上面代码会得到两个结果集，对照DatabaseMetaData 的文档，我们可以通过结果集的列名来获取想要的信息，例如
        while (tableSet.next()) {
            String tableName = tableSet.getString("TABLE_NAME");
            if (isConSpeCharacters(tableName)) {
                continue;
            }
            list.add(tableName);
        }
        tables = list;
        return list;
    }

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
        String driver = "com.mysql.jdbc.Driver";
        String username = "root";
        String password = "admin";
        DBUtil dbUtil = new DBUtil(url,username,password,driver);
        try {
            dbUtil.getConnection();
            dbUtil.getTableInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Tables getColumInfo(String tableName) throws SQLException {
        Tables table = new Tables();
        table.setName(tableName);
        for(String tname:tables){
            if(tname.equalsIgnoreCase(tableName)){
                ResultSet primaryKey = databaseMetaData.getPrimaryKeys(null, null, tableName);
                ResultSet columnSet = databaseMetaData.getColumns(null, "%",
                        tableName, "%");
                String pk = null;
                while (primaryKey.next()) {
                    pk = primaryKey.getString("COLUMN_NAME");
                }
                List<Columns> columns = new ArrayList<Columns>();
                while (columnSet.next()) {
                    String columnName = columnSet.getString("COLUMN_NAME");
                    String sqlType = columnSet.getString("DATA_TYPE");
                    String typeName = columnSet.getString("TYPE_NAME");
                    Columns col = new Columns(columnName,sqlType,typeName);
                    if (pk != null) {
                        if (pk.equals(columnName)) {
                            col.setStatus(1);
                            table.setPkColumns(col);
                        }
                    }
                    columns.add(col);
                    table.setColums(columns);
                }
            }

        }
        return table;
    }


}
